package com.hhs.storage.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.hhs.storage.bean.Contract;
import com.hhs.storage.bean.InputParam;
import com.hhs.storage.bean.ResponseInfo;
import com.hhs.storage.service.BaseLogsService;
import com.hhs.storage.service.ContractService;
import com.hhs.storage.util.SysUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 合同处理
 * @author hhs
 * @since 2022-11-30
 */
@RestController
@RequestMapping("/contract")
public class ContractController {
    @Resource
    private BaseLogsService baseLogsService;
    @Resource
    ContractService contractService;

    //合同录入
    //request 是为了获取token里的userid
    @PostMapping(value = "addOrEdit")
    public Map addOrEdit(Contract contract,
                   @RequestParam(value = "photoList",required = false)MultipartFile[] photoList,
                   String deleIdStr ,
                   HttpServletRequest request) {
        if (StringUtils.isEmpty(contract.getId())){
            baseLogsService.log("合同管理","新增",null);

        }else{
            baseLogsService.log("合同管理","编辑",null);

        }
        return contractService.addOrEdit(contract,request,deleIdStr,photoList);
    }

    @PostMapping(value = "dele")
    public Map dele(String id) {
        baseLogsService.log("合同管理","删除",null);

        return contractService.dele(id);
    }
    @RequestMapping(value = "query")
    public Map<String, Object> query(Contract contract,InputParam inputParam) throws IOException, ClassNotFoundException {
        baseLogsService.log("合同管理","查询",null);

        return contractService.query(contract,inputParam);
    }

    /**
     * 查询与自身相关合同  完成的或临时的
     * @param contract
     * @param inputParam
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    @RequestMapping(value = "queryBySelfContract")
    public Map<String, Object> queryBySelfContract(Contract contract,InputParam inputParam) throws IOException, ClassNotFoundException {
        return contractService.queryBySelfContract(contract,inputParam);
    }

    @RequestMapping(value = "table")
    public Map<String, Object> getAll(InputParam inputParam) throws IOException, ClassNotFoundException {
        baseLogsService.log("合同管理","查询",null);

        return contractService.getAll(inputParam);
    }

    //合同管理---我的录入
    @RequestMapping(value = "myType")
    public Map<String, Object> myType(HttpServletRequest request,InputParam inputParam) throws IOException, ClassNotFoundException {
        baseLogsService.log("合同管理","我的录入",null);

        String createrId = SysUtil.getLoginUserId(request);
        inputParam.setCurUserId(createrId);//当前用户是创建人
        inputParam.setQueryType(3);//我的录入，查询新建以及临时合同类型
        return contractService.getAll(inputParam);
    }
    //合同管理---临期合同
    @RequestMapping(value = "exp")
    public Map<String, Object> exp(HttpServletRequest request,InputParam inputParam) throws IOException, ClassNotFoundException {
        baseLogsService.log("合同管理","临期合同",null);
        //仅查询40条数据
        String createrId = SysUtil.getLoginUserId(request);
        inputParam.setCurUserId(createrId);
        List<Contract> list = contractService.getExpireCaution(inputParam);
        return ResponseInfo.success(list);
    }
    //合同管理---临时合同
    @RequestMapping(value = "temp")
    public Map<String, Object> temp(HttpServletRequest request,InputParam inputParam) throws IOException, ClassNotFoundException {
        baseLogsService.log("合同管理","临时合同",null);
        inputParam.setStatus(Contract.STATUS_TEMP);//临时合同 创建即结束
        String createrId = SysUtil.getLoginUserId(request);
        inputParam.setCurUserId(createrId);//当前用户是创建人
//        inputParam.setContractType(SysUtil.TEMP_CONTRACT_TYPEID);
        return contractService.getAll(inputParam);
    }
    //临期合同-不再提醒
    @RequestMapping(value = "exp/nowarn")
    public Map<String, Object> nowarn(HttpServletRequest request,InputParam inputParam) throws IOException, ClassNotFoundException {
        baseLogsService.log("合同管理","临期合同-设置不提醒",null);
        String createrId = SysUtil.getLoginUserId(request);
        inputParam.setCurUserId(createrId);
        contractService.setNoRemind(inputParam);
        return ResponseInfo.success();
    }
    @RequestMapping(value = "exp/defer")
    public Map<String, Object> defer(HttpServletRequest request,InputParam inputParam) throws IOException, ClassNotFoundException {
        baseLogsService.log("合同管理","临期合同-合同延期",null);
        String createrId = SysUtil.getLoginUserId(request);
        inputParam.setCurUserId(createrId);
        contractService.setDefer(inputParam);
        return ResponseInfo.success();
    }
    //合同相关类型选项数据
    @RequestMapping(value = "getAllOptions")
    public Map<String, Object> getAllOptions() {
        Map<String, Object> result=new HashMap<>();
        Map<String, Object> map=new HashMap<>();
        map.put("contractTypes",SysUtil.CONTRACT_TYPE_LIST);
        map.put("modelTypes",SysUtil.MODEL_TYPE_LIST);
        map.put("payTypes",SysUtil.PAY_TYPE_LIST);
        map.put("payWays",SysUtil.PAY_WAY_LIST);
        result.put("code", 1);
        result.put("data", map);
        return result;
    }
}

